package com.carkeeper.tweet.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @program: carkeeper_parent
 * @Date: 2020/11/25 10:29
 * @Author: qgg
 * @Description:
 */
@Configuration
public class RabbitMQConfig {

    //定义队列名称
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";

    //定义队列名称
    public static final String SEARCH_ADD_QUEUE = "search_add_queue";

    //定义上推队列名称
    public static final String SEARCH_DEL_QUEUE = "search_del_queue";

    //定义上推交换机
    public static final String TWEET_UP_EXCHANGE = "tweet_up_exchange";

    //定义下推交换机
    public static final String TWEET_DOWN_EXCHANGE = "tweet_down_exchange";


    //声明队列
    @Bean(AD_UPDATE_QUEUE)
    public Queue queue(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //声明上推队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue SEARCH_ADD_QUEUE(){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    //声明下推队列
    @Bean(SEARCH_DEL_QUEUE)
    public Queue SEARCH_DEL_QUEUE(){
        return new Queue(SEARCH_DEL_QUEUE);
    }

    //声明上推交换机
    @Bean(TWEET_UP_EXCHANGE)
     public Exchange TWEET_UP_EXCHANGE(){
         return ExchangeBuilder.fanoutExchange(TWEET_UP_EXCHANGE).durable(true).build();
     }

     //声明下推交换机
    @Bean(TWEET_DOWN_EXCHANGE)
    public Exchange TWEET_DOWN_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(TWEET_DOWN_EXCHANGE).durable(true).build();
    }

    //上推队列绑定到上推交换机中
    @Bean
    public Binding AD_UPDATE_QUEUE_BINDING(@Qualifier(SEARCH_ADD_QUEUE) Queue queue,
                                           @Qualifier(TWEET_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //下推队列绑定到上推交换机中
    @Bean
    public Binding TWEET_DOWN_EXCHANGE_BINDING(@Qualifier(SEARCH_DEL_QUEUE) Queue queue,
                                           @Qualifier(TWEET_DOWN_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

}
